package org.qu.userdao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.qu.userdao.IUserDao;
import org.qu.vo.User;

import com.opensymphony.xwork2.ActionContext;

/**
 * 用户管理实现类
 * @author 韩进城
 *
 */
public class UserManagerImpl implements IUserDao {
	private Connection conn;
	private PreparedStatement ps;
	private ResultSet rs;
	public UserManagerImpl(Connection connection){
		this.conn=connection;
	}
	/**
	 * 判断用户名和密码是否正确，ajax的方式请求
	 */
	public boolean isLogin(User user){
		// TODO Auto-generated method stub
		boolean flag=false;
		if(user!=null){
			 String sql="SELECT enname,email,cnname,sex,birthday FROM Users WHERE email=? AND password=?";
			try {
				 ps=conn.prepareStatement(sql);
				 ps.setString(1, user.getEmail());
				 ps.setString(2,user.getPassword());
				 rs=ps.executeQuery();
				 if(rs.next()){
					 User uu=new User();
					 uu.setEnname(rs.getString("enname"));
					 uu.setEmail(rs.getString("email"));
					 uu.setCnname(rs.getString("cnname"));
					 uu.setSex(rs.getString("sex"));
					 uu.setBirthday(rs.getDate("birthday"));
					 flag=true;
				 }
			} catch (Exception e) {
				// TODO: handle exception
			}finally{
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return flag;
	}

	/**
	 * 新建用户
	 */
	public boolean doCreate(User vo){
		// TODO Auto-generated method stub
		boolean flag=false;
		if(vo!=null){
			try {
				String sql="INSERT INTO Users(email,password) values (?,?)";
				ps=conn.prepareStatement(sql);
				ps.setString(1, vo.getEmail());
				ps.setString(2, vo.getPassword());
				int i=ps.executeUpdate();
				if(i>0){
					flag=true;
				}
			} catch (Exception e) {
				// TODO: handle exception
			}finally{
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return flag;
	}

	public boolean doRemove(User id){
		// TODO Auto-generated method stub
		return false;
	}

	public boolean doUpdate(User vo){
		// TODO Auto-generated method stub
		return false;
	}

	public List<User> findAll(String kw){
		// TODO Auto-generated method stub
		return null;
	}

	public List<User> findAll(String kw, Integer cp, Integer ls){
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * id值传入Email
	 */
	public User findById(User id){
		// TODO Auto-generated method stub
		User user=null;
		String sql="SELECT email,password FROM users WHERE email=?";
		try {
			ps=conn.prepareStatement(sql);
			ps.setString(1, id.getEmail());
			rs=ps.executeQuery();
			if(rs.next()){
				user=new User();
				user.setEmail(rs.getString("email"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				rs.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return user;
	}

	public Integer getAllCount(String kw){
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 验证用户名是否存在
	 */
	public boolean isName(String name) {
		// TODO Auto-generated method stub
		boolean flag=false;
		if(name!=null && !"".equals(name)){
			String sql="SELECT * FROM Users WHERE email=?";
			try {
				ps=conn.prepareStatement(sql);
				ps.setString(1,name);
				rs=ps.executeQuery();
				if(rs.next()){
					flag=true;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		return flag;
	}
}
